package com.example.clock;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import com.example.clock.R;

public class MainActivity extends Activity {
    private TabHost tabHost;
    private TextView timeTextView;
    private TextView dateTextView;
    private Switch timeFormatSwitch;
    private Button btnSelectDate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabHost=(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();

        tabHost.addTab(tabHost.newTabSpec("tabAlarm").setIndicator("闹钟").setContent(R.id.tabAlarm));
        tabHost.addTab(tabHost.newTabSpec("tabTime").setIndicator("时钟").setContent(R.id.tabTime));
        tabHost.addTab(tabHost.newTabSpec("tabTimer").setIndicator("计时器").setContent(R.id.tabTimer));
        tabHost.addTab(tabHost.newTabSpec("tabStopWatch").setIndicator("秒表").setContent(R.id.tabStopWatch));
        timeTextView = findViewById(R.id.timeTextView);
        dateTextView = findViewById(R.id.dateTextView);
        timeFormatSwitch = findViewById(R.id.timeFormatSwitch);

        // 初始显示时间和日期
        updateTimeAndDate();

        // 设置切换开关的监听器
        timeFormatSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                updateTimeAndDate();
            }
        });

        // 开启定时器，每秒更新一次时间和日期显示
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                updateTimeAndDate();
                            }
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }

    private void updateTimeAndDate() {
        Calendar calendar = Calendar.getInstance();
        Date currentDate = calendar.getTime();

        // 根据切换开关状态选择时间格式
        SimpleDateFormat timeFormat;
        if (timeFormatSwitch.isChecked()) {
            timeFormat = new SimpleDateFormat("hh:mm:ss a", Locale.getDefault());
        } else {
            timeFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
        }

        // 格式化时间并显示
        String timeText = timeFormat.format(currentDate);
        timeTextView.setText(timeText);

        // 格式化日期并显示
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE", Locale.getDefault());
        String dateText = dateFormat.format(currentDate);
        dateTextView.setText(dateText);
    }

    }

